package com.iwhalecloud.bss.kite.cucc.aspect;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import com.iwhalecloud.bss.kite.common.util.GrayUtil;

import com.ztesoft.bss.rul.core.client.dto.kite.KiteRuleRequest;
import com.ztesoft.bss.rul.core.client.dto.rule.InputObject;

/**
 * @ClassName : GrayRuleAspect
 * @Description : 给规则透传灰度路由信息
 * @Author : xie.zhengjun
 * @Date : 2021/5/18 11:48
 */
@Aspect
@Component
public class GrayRuleAspect {
    @Pointcut("execution(public com.ztesoft.bss.rul.core.client.dto.kite.KiteRuleResponse com.ztesoft.bss.rul.core.api.provider.KiteRuleEngine.run(com.ztesoft.bss.rul.core.client.dto.kite.KiteRuleRequest,com.ztesoft.bss.rul.core.client.dto.rule.InputObject))")
    public void pointCut() {
    }

    @Around(value = "pointCut() && args(request, inputObject)", argNames = "point,request,inputObject")
    public Object addControlNode(ProceedingJoinPoint point, KiteRuleRequest request, InputObject inputObject)
        throws Throwable {
        Signature signature = point.getSignature();
        if (!(signature instanceof MethodSignature)) {
            return point.proceed();
        }
        Map<String, Object> params;
        params = inputObject.getParams();
        if (Objects.isNull(params)) {
            params = new HashMap<>();
            inputObject.setParams(params);
        }
        GrayUtil.addControlNode(params);
        return point.proceed();
    }
}
